#!/usr/bin/env python3
"""
智能绘图助手启动脚本
"""

import os
import sys
import subprocess
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def check_dependencies():
    """检查依赖是否安装"""
    try:
        import streamlit
        import psycopg2
        import geopandas
        import matplotlib
        import seaborn
        import plotly
        import PIL
        import imageio
        import langchain
        logger.info("✅ 所有依赖已正确安装")
        return True
    except ImportError as e:
        logger.error(f"❌ 缺少依赖: {e}")
        logger.error("请运行: pip install -r requirements.txt")
        return False

def check_environment():
    """检查环境配置"""
    # 检查LLM配置
    try:
        from config import LLM_CONFIG
        if not LLM_CONFIG.get('openai_api_key'):
            logger.warning("⚠️ LLM配置中缺少API密钥")
            return False
        logger.info("✅ 环境配置检查通过")
        return True
    except ImportError:
        logger.error("❌ 无法导入配置文件")
        return False

def create_directories():
    """创建必要的目录"""
    directories = ['outputs', 'temp']
    for directory in directories:
        if not os.path.exists(directory):
            os.makedirs(directory)
            logger.info(f"📁 创建目录: {directory}")

def main():
    """主函数"""
    logger.info("🎨 启动智能绘图助手")
    
    # 检查依赖
    if not check_dependencies():
        sys.exit(1)
    
    # 检查环境配置
    if not check_environment():
        logger.warning("⚠️ 环境配置不完整，但仍可运行（部分功能可能受限）")
    
    # 创建必要目录
    create_directories()
    
    # 启动Streamlit应用
    try:
        logger.info("🚀 启动Web应用...")
        subprocess.run([
            sys.executable, '-m', 'streamlit', 'run', 'app.py',
            '--server.port', '8501',
            '--server.address', 'localhost'
        ])
    except KeyboardInterrupt:
        logger.info("👋 应用已停止")
    except Exception as e:
        logger.error(f"❌ 启动失败: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main() 